package center.wxp.log.config;

import center.wxp.log.core.LogService;
import center.wxp.log.properties.LogProperties;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wxp
 * 项目请求入参返回参数自动配置
 * EnableConfigurationProperties: 注解告诉Spring Boot 能支持@ConfigurationProperties
 * ConditionalOnClass: 当类路径classpath下有指定的类的情况下进行自动配置
 * ConditionalOnProperty: 当配置文件中center.wxp.log.enabled=true时进行自动配置，
 * 如果没有设置此值就默认使用matchIfMissing对应的值,true:加载配置，false不加载配置。
 * ConditionalOnMissingBean: 当容器中没有指定Bean的情况下，自动配置LogService类
 * 2020/3/12
 */
@Configuration
@EnableConfigurationProperties(LogProperties.class)
@ConditionalOnClass(Logger.class)
@ConditionalOnProperty(prefix = "center.wxp.log", value = "enabled", matchIfMissing = false)
public class LogAutoConfiguration {
    @Autowired
    private LogProperties logProperties;

    @Bean
    //当容器中没有这个Bean时(LogService)就自动配置这个Bean，Bean的参数来自于BambooServerProperties
    @ConditionalOnMissingBean(LogService.class)
    public LogService logService() {
        LogService logService = new LogService(logProperties);
        return logService;
    }
}
